﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace FormBase
{
    partial class FAboutListView : Form
    {
        public FAboutListView()
        {
            InitializeComponent();
            //this.Text = String.Format("About {0}", AssemblyTitle);
            //this.labelProductName.Text = AssemblyProduct;
            //this.labelVersion.Text = String.Format("Version {0}", AssemblyVersion);
            //this.labelCopyright.Text = AssemblyCopyright;
            //this.labelCompanyName.Text = AssemblyCompany;
            //this.textBoxDescription.Text = AssemblyDescription;
        }

        #region Assembly Attribute Accessors

        public string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (titleAttribute.Title != "")
                    {
                        return titleAttribute.Title;
                    }
                }
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion

        private void okButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FAboutListView_Load(object sender, EventArgs e)
        {
            listView1.HeaderStyle = ColumnHeaderStyle.Clickable;
            listView1.View = View.Details;
            listView1.Columns.Clear();
            listView1.Columns.Add(string.Empty);
            listView1.Columns.Add("Project");
            listView1.Columns.Add("Version");
            listView1.Columns[0].Width = 10;
            listView1.Columns[1].Width = 180;
            listView1.Columns[2].Width = 300;

            listView2.HeaderStyle = ColumnHeaderStyle.Clickable;
            listView2.View = View.Details;
            listView2.Columns.Clear();
            listView2.Columns.Add(string.Empty);
            listView2.Columns.Add("Environment");
            listView2.Columns.Add("Version");
            listView2.Columns[0].Width = 10;
            listView2.Columns[1].Width = 180;
            listView2.Columns[2].Width = 300;

            ListViewItem listItem = new ListViewItem();
            listItem.Text = "";
            listItem.SubItems.Add("ProductName");
            listItem.SubItems.Add(AssemblyProduct);
            listView1.Items.Insert(0, listItem);

            listItem = new ListViewItem();
            listItem.Text = "";
            listItem.SubItems.Add("Version");
            listItem.SubItems.Add(String.Format("Version {0}", AssemblyVersion));
            listView1.Items.Insert(1, listItem);

            listItem = new ListViewItem();
            listItem.Text = "";
            listItem.SubItems.Add("Copyright");
            listItem.SubItems.Add(AssemblyCopyright);
            listView1.Items.Insert(2, listItem);

            listItem = new ListViewItem();
            listItem.Text = "";
            listItem.SubItems.Add("CompanyName");
            listItem.SubItems.Add(AssemblyCompany);
            listView1.Items.Insert(3, listItem);

            Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
            int index = 0;
            foreach (AssemblyName an in asm.GetReferencedAssemblies())
            {
                listItem = new ListViewItem();
                listItem.Text = "";
                listItem.SubItems.Add(an.Name);
                listItem.SubItems.Add(string.Format("{0}", an.Version));
                listView2.Items.Insert(index, listItem);

                index++;
            }

        }
    }
}
